from django.db import models
from pygments.lexers import get_all_lexers
from pygments.styles import get_all_styles
from pygments.lexers import get_lexer_by_name
from pygments.formatters.html import HtmlFormatter
from pygments import highlight

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted([(item, item) for item in get_all_styles()])



class Album(models.Model):

    album_name = models.CharField(max_length=100)
    artist = models.CharField(max_length=100)

    class Meta:
        db_table = 'tb_album'
        verbose_name = '专辑'
        verbose_name_plural = verbose_name


class Track(models.Model):
    album = models.ForeignKey(Album, related_name='tracks', on_delete=models.CASCADE)
    order = models.IntegerField()
    title = models.CharField(max_length=100)
    duration = models.IntegerField()

    class Meta:
        db_table = 'tb_track'
        verbose_name = 'tack'
        verbose_name_plural = verbose_name
        unique_together = ['album', 'order']
        ordering = ['order']


    def __str__(self):

        return '%d：%s' % (self.order, self.title)


class Book(models.Model):

    title = models.CharField(max_length=50, verbose_name='书名')
    content = models.CharField(max_length=200, verbose_name='内容')

    class Meta:
        db_table = 'tb_book'
        verbose_name = '书名'
        verbose_name_plural = verbose_name

class Comment(models.Model):

    email = models.EmailField(verbose_name='邮箱')

    content = models.CharField(verbose_name='内容', max_length=200)

    created = models.DateTimeField(auto_now_add=True, verbose_name='日期')


    class Meta:

        db_table = 'tb_comment'
        verbose_name = '评论表'
        verbose_name_plural = verbose_name


class Snippet(models.Model):

    owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
    highlighted = models.TextField(default='')
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)

    class Meta:

        ordering = ['created']


    def save(self, *args, **kwargs):
        lexer = get_lexer_by_name(self.language)
        linenos = 'table' if self.linenos else False
        options = {'title': self.title} if self.title else {}
        formatter = HtmlFormatter(style=self.style, linenos=linenos, full=True, **options)
        self.highlighted = highlight(self.code, lexer, formatter)
        super(Snippet, self).save(*args, **kwargs)



class HighScore(models.Model):

    score = models.IntegerField(verbose_name='分数')
    player_name = models.CharField(max_length=30, verbose_name='游戏玩家')

    class Meta:

        db_table = 'tb_highscore'
        verbose_name = '分数表'
        verbose_name_plural = verbose_name











